//
// Created by zhouyi on 2019/9/10.
//
#include <stdio.h>
#include <stdlib.h>

void test_0();

void test_1();

void test_2();


void swap(int *pA, int *pB);

int main() {
//    test_0();
//    test_1();
    test_2();

    return 0;
}

void test_2() {
    int a = 10, b = 20;
    swap(&a, &b);
}

void swap(int *pA, int *pB) {
    printf("swap before address pA = %p, pB = %p \n", pA, pB);
    printf("swap before value A = %d, B = %d \n", *pA, *pB);

    int temp = *pA;
    *pA = *pB;
    *pB = temp;

    printf("swap after address pA = %p, pB = %p \n", pA, pB);
    printf("swap after value A = %d, B = %d \n", *pA, *pB);
}


void test_1() {
    int a = 258;
    int b = 10000;
    int *ptr_a = &a;

    printf("a = %p \n", &a);
    printf("b = %p \n", &b);

    // 指针本身的地址
    printf("ptr_a自己的地址 = %p \n", &ptr_a);

    // malloc分配内存
    ptr_a = (int *) malloc(sizeof(int));

    printf("ptr_a自己的地址 = %p \n", &ptr_a);
    // 指针所指向的变量的内存地址
    printf("ptr_a = %p \n", ptr_a);

    free(ptr_a);
    // 如果不NULL，这个被free的指针叫做悬空指针，和野指针一样危险
    // 0x0地址，一般不能被访问
    ptr_a = NULL;
    printf("ptr_a = %p \n", ptr_a);

    // &,用于取一个对象的地址
    // *是一个间接寻址符，用于访问指针所指向的地址的值
    // int *ptr 定义一个指针
    // & 与 * 是一对互逆运算



    int var = 222, *pVar;
    pVar = &var;
    int temp = *pVar;
    printf("===================================\n");
    printf("var = %d \n", var);
    printf("*pVar = %d \n", *pVar);
    printf("temp = %d \n", *pVar);

}

void test_0() {
    int i = 128;
    int *ptr_i = &i;


    printf("%p \n", &i);
    printf("%p \n", ptr_i);
    printf("*ptr_i=%d\n", *ptr_i);
    printf("*ptr_i自己的地址 = %p \n", &ptr_i);

    printf("char* size = %d\n", sizeof(char *));
    printf("int* size = %d\n", sizeof(int *));
    printf("void* size = %d\n", sizeof(void *));
    printf("long long* size = %d\n", sizeof(long long *));
}

